package com.situ.gym.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.situ.gym.pojo.Course;
import com.situ.gym.pojo.query.CourseQuery;
import com.situ.gym.service.ICourseService;
import com.situ.gym.service.ICourseService;
import com.situ.gym.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Gao
 * @since 2025-07-09
 */
@RestController
@RequestMapping("/course")
public class CourseController {
    //注入过来service
    @Autowired
    private ICourseService courseService;

    //Restful    GET(查找)、POST(添加)、DELETE(删除)、PUT(修改)
    @GetMapping("/list")
    public Result list(CourseQuery courseQuery) {
        IPage<Course> page = courseService.list(courseQuery);
        return Result.ok(page);
    }

    // /course/deleteById/12
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        courseService.removeById(id);
        return Result.ok("删除成功");
    }

    @DeleteMapping("/deleteAll/{ids}")
    public Result deleteAll(@PathVariable Integer[] ids) {
        courseService.removeBatchByIds(Arrays.asList(ids));
        return Result.ok("删除成功");
    }

    @PostMapping("/add")
    public Result add(@RequestBody Course course) {
        courseService.save(course);
        return Result.ok("添加成功");
    }

    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Course course = courseService.getById(id);
        return Result.ok(course);
    }

    @PutMapping("/update")
    public Result update(@RequestBody Course course) {
        // update course set name='zhangsn',age=23 where id=1
        courseService.updateById(course);
        return Result.ok("修改成功");
    }

}

